package com.cn.medicalcare.service.serviceimpl;

import com.cn.medicalcare.customtool.JedisUtil;
import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.AdvertisementMapper;
import com.cn.medicalcare.mapper.CompanyIntroduceMapper;
import com.cn.medicalcare.mapper.ProductInfoMapper;
import com.cn.medicalcare.pojo.AllPage;
import com.cn.medicalcare.pojo.CompanyIntroduce;
import com.cn.medicalcare.pojo.CompanyIntroduce;
import com.cn.medicalcare.service.CompanyIntroduceService;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

public class CompanyIntroduceServiceImpl implements CompanyIntroduceService {

    private static final String COMPANY_CACHE_KEY = "survival_company";

    //静态内部类单例模式
    private static class SingletonClassInstance {
        private static final CompanyIntroduceServiceImpl instance = new CompanyIntroduceServiceImpl();
    }

    private CompanyIntroduceServiceImpl() {
    }

    public static CompanyIntroduceServiceImpl getInstance() {
        return CompanyIntroduceServiceImpl.SingletonClassInstance.instance;
    }

    @Override
    public boolean insetCompanyIntroduce(CompanyIntroduce companyIntroduce) {
        companyIntroduce.setSurvivalupdatetime(new Date());
        boolean insertFlag = SqlSessionFactoryTool.DoMapperAndCommit(CompanyIntroduceMapper.class, m -> m.insertCompanyIntroduce(companyIntroduce));
        if (insertFlag) {
            JedisUtil.jedisDo(j -> j.del(COMPANY_CACHE_KEY));
        }
        return insertFlag;
    }

    @Override
    public boolean updateCompanyIntroduce(CompanyIntroduce companyIntroduce) {
        if (companyIntroduce.getSurvival() == 1) {
            companyIntroduce.setSurvivalupdatetime(new Date());
        }
        boolean updateFlag = SqlSessionFactoryTool.DoMapperAndCommit(CompanyIntroduceMapper.class, m -> m.updateCompanyIntroduce(companyIntroduce));
        if (updateFlag) {
            JedisUtil.jedisDo(j -> j.del(COMPANY_CACHE_KEY));
        }
        return updateFlag;
    }

    @Override
    public List<CompanyIntroduce> selectCompanyIntroduceInPage(AllPage<CompanyIntroduce> allPageCompanyIntroduce) {
        List<CompanyIntroduce> list = SqlSessionFactoryTool.DoMapper(CompanyIntroduceMapper.class, m -> m.selectCompanyIntroduceInPage(allPageCompanyIntroduce));
        return list;
    }

    @Override
    public int selectCompanyIntroduceCount(CompanyIntroduce companyIntroduce) {
        return SqlSessionFactoryTool.DoMapper(CompanyIntroduceMapper.class, m -> m.selectCompanyIntroduceCount(companyIntroduce));
    }

    @Override
    public CompanyIntroduce selectAboutUsCompanyIntroduce() {
        CompanyIntroduce companyIntroduce = null;
        try {
            String companyIntroducejson = JedisUtil.jedisDo(j -> j.get(COMPANY_CACHE_KEY));
            if (StringUtils.isNotEmpty(companyIntroducejson)) {
                companyIntroduce = JsonTool.toObject(companyIntroducejson, CompanyIntroduce.class);
                JedisUtil.jedisDo(j -> j.expire(COMPANY_CACHE_KEY, JedisUtil.CACHE_TIME));
            }
        } catch (Exception e) {
        }
        if (companyIntroduce == null) {
            companyIntroduce = SqlSessionFactoryTool.DoMapper(CompanyIntroduceMapper.class, m -> m.selectAboutUsCompanyIntroduce());
            try {
                String companyIntroducejson = JsonTool.toJson(companyIntroduce);
                JedisUtil.jedisDo(j -> j.setex(COMPANY_CACHE_KEY, JedisUtil.CACHE_TIME, companyIntroducejson));
            } catch (Exception e) {
            }
        }
        return companyIntroduce;
    }
}
